package de.klingbeil.hhc.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.google.gwt.user.client.ui.Widget;

import de.klingbeil.hhc.client.GridFactory;
import de.klingbeil.hhc.client.model.ItemModelData;

public class ItemListWidget extends LayoutContainer {

	private LayoutContainer rootContainer;
	private Grid<ItemModelData> grid;

	public ItemListWidget() {
		createWidgets();
	}

	public Widget getWidget() {
		return this;
	}

	public void setItems(List<ItemModelData> items) {
		ListStore<ItemModelData> store = grid.getStore();
		store.removeAll();
		store.add(items);
		layout();
	}

	private void createWidgets() {
		addRootContainer();
		addGrid();
	}

	private void addRootContainer() {
		rootContainer = new LayoutContainer();
		rootContainer.addStyleName("hhc-item-list-root-container");
		add(rootContainer);
	}

	private void addGrid() {
		createGrid();
		rootContainer.add(grid);
	}

	private Widget createGrid() {
		ListStore<ItemModelData> store = new ListStore<ItemModelData>();
		List<ColumnConfig> config = new ArrayList<ColumnConfig>();

		addDescriptionColumn(config);

		ColumnModel cm = new ColumnModel(config);

		grid = GridFactory.createGrid(store, cm);
		grid.addStyleName("hhc-list");
		grid.setHeight(500);
		return grid;
	}

	private static void addDescriptionColumn(List<ColumnConfig> configs) {
		ColumnConfig catalogs = new ColumnConfig("description",
				"Item Description", 300);
		configs.add(catalogs);
	}

}
